import { ElMessage } from 'element-plus';
import { deepCopy } from '/@/utils';
import { useProductApi } from '/@/api/product';
const { getDimensionList, editDimension } = useProductApi();

export const useInit = () => {
	const loading = ref(false);
	const tableData = ref([]);
	const isAdd = ref(false);
	const itemInfo = ref({});
	const page = ref({
		pageSize: 10,
		current: 1,
		total: 0,
	});
	const searchForm = ref({
		status: '',
	});
	const init = async () => {
		loading.value = true;
		let res = await getDimensionList({ ...page.value, ...searchForm.value });
		loading.value = false;
		if (res.code !== 200) return;
		const { data, total } = res.data;
		tableData.value = data;
		page.value.total = total;
	};
	onMounted(init);
	const addDialogVisible = ref(false);
	const toAdd = () => {
		addDialogVisible.value = true;
		isAdd.value = true;
	};
	const toEdit = (row) => {
		itemInfo.value = deepCopy(row);
		addDialogVisible.value = true;
		isAdd.value = false;
	};
	const changeStatus = async ({ id, status }) => {
		let res = await editDimension({ id, status });
		if (res.code === 200) {
			ElMessage.success('更改状态成功！');
		} else {
			init();
		}
	};
	return {
		addDialogVisible,
		tableData,
		isAdd,
		itemInfo,
		page,
		searchForm,
		loading,
		init,
		toAdd,
		toEdit,
		changeStatus,
	};
};
